package model;

import java.awt.*;
import java.util.*;

import view.*;

public class Pelota extends Elemento {

	public int velX;
	public int velY;
	
	private Ventana ventana;
	
	public boolean gol = false;
	
	public Pelota(int x, int y, int width, int height, int velX, int velY, Ventana ventana) {
		super(x, y, width, height);
		
		this.setVelX(velX);
		this.setVelY(velY);
		
		this.ventana = ventana;
	}
	
	public void pintar(Graphics g) {
		g.setColor(Color.WHITE);
		g.fillRect(this.getX(), this.getY(), this.getWidth(), this.getHeight());
	}

	public void mover() {

		Iterator<Elemento> iterador = ventana.elementosColisionables.iterator();
		
		boolean choquePared = false;
		boolean choqueJugador = false;
		
        for (; iterador.hasNext();) {
        	Elemento actor = iterador.next();
        	if(this.crearRectangulo().intersects(actor.crearRectangulo())) {
        		if(actor instanceof Pared) {
        			choquePared = true;
        		}
        		if(actor instanceof Jugador) {
        			choqueJugador = true;
        		}
        	}
        }   
		
        if(choquePared) {
        	velY = -velY;
        } 
        
        if(choqueJugador) {
        	velX = -velX;
        }
        
		this.x = this.x + this.velX;
		this.y = this.y + this.velY;
		
		if(huboGol()) {
			this.setX(ventana.getWidth()/2);
			this.setY(ventana.getHeight()/2);
		}
	}
	
	public boolean huboGol() {
		if(x > 800) {
			gol = true;
			Ventana.marcadorIzquierda++;
		} else if (x < 0) {
			gol = true;
			Ventana.marcadorDerecha++;
		} else {
			gol = false;
		}
		
		return gol;
	}
	
	public int getVelX() {
		return velX;
	}

	public void setVelX(int velX) {
		this.velX = velX;
	}

	public int getVelY() {
		return velY;
	}

	public void setVelY(int velY) {
		this.velY = velY;
	}


}
